import { credentials, Metadata, MetadataValue } from "grpc";
import {
  canUse,
  isUndefined,
  BackendServices,
  StringIndexerTo,
  _pairsT,
} from "mmb";
import { GrpcBgsClientKeys } from "./grpcClientKeys";
import { getGrpcClient, setGrpcClient } from "../store/helper";
import { StatusOpClient } from "./util/status_grpc_pb";
import { GsmsProxyOpClient } from "./util/gsms_proxy_grpc_pb";
import { UserOpClient } from "./service/user_grpc_pb";
import { UserPvLogOpClient } from "./service/user_pv_log_grpc_pb";
import { UserFanOpClient } from "./service/user_fan_grpc_pb";
import { UserFollowOpClient } from "./service/user_follow_grpc_pb";
import { ActivityOpClient } from "./service/activity_grpc_pb";
import { ActivityStatOpClient } from "./service/activity_stat_grpc_pb";
import { ActivityUserAwardOpClient } from "./service/activity_user_award_grpc_pb";
import { ActivityUserProcedureOpClient } from "./service/activity_user_procedure_grpc_pb";
import { ActivityUserPvLogOpClient } from "./service/activity_user_pv_log_grpc_pb";
import { BrandOpClient } from "./service/brand_grpc_pb";
import { BrandAdminOpClient } from "./service/brand_admin_grpc_pb";
import { BrandDistributeRequestOpClient } from "./service/brand_distribute_request_grpc_pb";
import { BrandDistributorOpClient } from "./service/brand_distributor_grpc_pb";
import { BrandEndorseRequestOpClient } from "./service/brand_endorse_request_grpc_pb";
import { BrandFanOpClient } from "./service/brand_fan_grpc_pb";
import { BrandFollowOpClient } from "./service/brand_follow_grpc_pb";
import { BrandBankCardOpClient } from "./service/brand_bank_card_grpc_pb";
import { BrandSpokesmanOpClient } from "./service/brand_spokesman_grpc_pb";
import { BrandWarehouseOpClient } from "./service/brand_warehouse_grpc_pb";
import { CommonOpClient } from "./service/common_grpc_pb";
import { WxpayApplymentOpClient } from "./service/wxpay_applyment_grpc_pb";
import { OrderOpClient } from "./service/order_grpc_pb";
import { AfterSaleOpClient } from "./service/after_sale_grpc_pb";
import { ProductGroupOpClient } from "./service/product_group_grpc_pb";
import { ProductOpClient } from "./service/product_grpc_pb";
import { StoreOpClient } from "./service/store_grpc_pb";
import { StoreFanOpClient } from "./service/store_fan_grpc_pb";
import { StoreFollowOpClient } from "./service/store_follow_grpc_pb";
import { StoreEndorseRequestOpClient } from "./service/store_endorse_request_grpc_pb";
import { StoreSpokesmanOpClient } from "./service/store_spokesman_grpc_pb";
import { SyncOpClient } from "./service/sync_grpc_pb";
import { BgsVendorServiceClient as ThirdpartyOpClient } from "./others/thirdparty_grpc_pb"
import { LdAccountClient as AccountProxy } from "./proxy/account_grpc_pb"
import { BilinVendorClient as BilinAdminVendorProxy } from "./proxy/bilinadmin_grpc_pb"
import { SMSClient as GsmsProxy } from "./proxy/gsms_grpc_pb"
import { InventoryClient as InventoryProxy } from "./proxy/inventory_grpc_pb";
import { WxpayClient as WxpayProxy } from "./proxy/wxpay_grpc_pb"
import { getGrpcAddress } from "../store/helper";
import GrpcTypes from "./grpcTypes";

export const wrapperGrpcError = (msg: string | undefined) => {
  return new Error(`服务端错误:${msg||'err'}`)
}

export const getGrpcDeadline = (t: GrpcTypes) => {
  let timeAllowed = 10000;
  switch (t) {
    case GrpcTypes.LIGHT:
      timeAllowed = 10000; // LIGHT RPC
      break;
    case GrpcTypes.HEAVY:
      timeAllowed = 70000; // HEAVY RPC 70s
      break;
    case GrpcTypes.HEALTH:
      timeAllowed = 15000; // HEAVY RPC
      break;
    default:
      console.log("Invalid RPC Type: Using Default Timeout");
  }
  return new Date(Date.now() + timeAllowed);
};

export const getMetadata = (
  ext: StringIndexerTo<MetadataValue> | undefined = undefined
) => {
  const md = new Metadata();
  md.add("x-request-id", (+new Date()).toString());
  if (!isUndefined(ext)) {
    _pairsT<MetadataValue>(ext).forEach(({ k, v }) => {
      md.set(k, v);
    });
  }
  return md;
};

export const getOptionsOfLight = (options: object | undefined = undefined) => {
  return getOptions(GrpcTypes.LIGHT, options)
};

export const getOptionsOfHeavy = (options: object | undefined = undefined) => {
  return getOptions(GrpcTypes.HEAVY, options)
};

export const getOptions = (t: GrpcTypes, options: object | undefined = undefined) => {
  return Object.assign(
    { deadline: getGrpcDeadline(t) },
    options
  );
};

/****************************************************************************************/
/*                                                                                      */
/*                               account proxy                                          */
/*                                                                                      */
/****************************************************************************************/

export const registerAccountProxy = () => {
  const srv = BackendServices.ACCOUNT;
  let cl = getAccountProxy();
  if (cl == null || cl === undefined) {
    cl = new AccountProxy(getGrpcAddress(srv)()!, credentials.createInsecure());
    setGrpcClient(srv)("account", cl);
  }
  return cl;
};

export const getAccountProxy = () => {
  const cl = getGrpcClient(BackendServices.ACCOUNT)("account");
  if (canUse(cl)) {
    return cl as AccountProxy;
  }
  return null;
};

export const getAccountProxySafely = () => {
  let cl = getAccountProxy();
  if (!canUse(cl)) {
    cl = registerAccountProxy();
  }
  return cl!
};

/****************************************************************************************/
/*                                                                                      */
/*                               bilinadmin vendor proxy                                          */
/*                                                                                      */
/****************************************************************************************/

export const registerBilinAdminVendorProxy = () => {
  const srv = BackendServices.BILIN_ADMIN;
  let cl = getBilinAdminVendorProxy();
  if (cl == null || cl === undefined) {
    console.log('getGrpcAddress(srv)()=>', getGrpcAddress(srv)())
    cl = new BilinAdminVendorProxy(getGrpcAddress(srv)()!, credentials.createInsecure());
    setGrpcClient(srv)("vendor", cl);
  }
  return cl;
};

export const getBilinAdminVendorProxy = () => {
  const cl = getGrpcClient(BackendServices.BILIN_ADMIN)("vendor");
  if (canUse(cl)) {
    return cl as BilinAdminVendorProxy;
  }
  return null;
};

export const getBilinAdminVendorProxySafely = () => {
  let cl = getBilinAdminVendorProxy();
  if (!canUse(cl)) {
    cl = registerBilinAdminVendorProxy();
  }
  return cl!
};

/****************************************************************************************/
/*                                                                                      */
/*                               gsms proxy                                             */
/*                                                                                      */
/****************************************************************************************/

export const registerGsmsProxy = () => {
  const srv = BackendServices.GSMS;
  let cl = getGsmsProxy();
  if (cl == null || cl === undefined) {
    cl = new GsmsProxy(getGrpcAddress(srv)()!, credentials.createInsecure());
    setGrpcClient(srv)("gsms", cl);
  }
  return cl;
};

export const getGsmsProxy = () => {
  const cl = getGrpcClient(BackendServices.GSMS)("gsms");
  if (canUse(cl)) {
    return cl as GsmsProxy;
  }
  return null;
};

export const getGsmsProxySafely = () => {
  let cl = getGsmsProxy();
  if (!canUse(cl)) {
    cl = registerGsmsProxy();
  }
  return cl!
};


/****************************************************************************************/
/*                              inventory proxy                                         */
/****************************************************************************************/

export const registerInventoryProxy = () => {
  const srv = BackendServices.INVENTORY;
  let cl = getInventoryProxy();
  if (cl == null || cl === undefined) {
    cl = new InventoryProxy(
      getGrpcAddress(srv)()!,
      credentials.createInsecure()
    );

    setGrpcClient(srv)("inventory", cl);
  }
  return cl;
};

export const getInventoryProxy = () => {
  const cl = getGrpcClient(BackendServices.INVENTORY)("inventory");
  if (canUse(cl)) {
    return cl as InventoryProxy;
  }
  return null;
};

export const getInventoryProxySafely = () => {
  let cl = getInventoryProxy();
  if (!canUse(cl)) {
    cl = registerInventoryProxy();
  }
  return cl!
};


/****************************************************************************************/
/*                              wxpay proxy                                             */
/****************************************************************************************/

export const registerWxpayProxy = () => {
  const srv = BackendServices.WXPAY;
  let cl = getWxpayProxy();
  if (cl == null || cl === undefined) {
    cl = new WxpayProxy(
      getGrpcAddress(srv)()!,
      credentials.createInsecure()
    );

    setGrpcClient(srv)("wxpay", cl);
  }
  return cl;
};

export const getWxpayProxy = () => {
  const cl = getGrpcClient(BackendServices.WXPAY)("wxpay");
  if (canUse(cl)) {
    return cl as WxpayProxy;
  }
  return null;
};

export const getWxpayProxySafely = () => {
  let cl = getWxpayProxy();
  if (!canUse(cl)) {
    cl = registerWxpayProxy();
  }
  return cl!
};

/****************************************************************************************/
/*                                                                                      */
/*                               bgs                                                    */
/*                                                                                      */
/****************************************************************************************/

/****************************************************************************************/
/*                               bgs/util status                                        */
/****************************************************************************************/

export const registerBgsStatusOp = () => {
  const srv = BackendServices.BGS;
  let cl = getBgsStatusOp();
  if (cl == null || cl === undefined) {
    cl = new StatusOpClient(
      getGrpcAddress(srv)()!,
      credentials.createInsecure()
    );

    setGrpcClient(srv)(GrpcBgsClientKeys.StatusOp, cl);
  }
  return cl;
};

export const getBgsStatusOp = () => {
  const cl = getGrpcClient(BackendServices.BGS)(GrpcBgsClientKeys.StatusOp);
  if (canUse(cl)) {
    return cl as StatusOpClient;
  }
  return null;
};

/****************************************************************************************/
/*                               bgs/util gsms_proxy                                    */
/****************************************************************************************/

export const registerBgsGsmsProxyOp = () => {
  const srv = BackendServices.BGS;
  let cl = getBgsGsmsProxyOp();
  if (cl == null || cl === undefined) {
    cl = new GsmsProxyOpClient(
      getGrpcAddress(srv)()!,
      credentials.createInsecure()
    );

    setGrpcClient(srv)(GrpcBgsClientKeys.GsmsProxyOp, cl);
  }
  return cl;
};

export const getBgsGsmsProxyOp = () => {
  const cl = getGrpcClient(BackendServices.BGS)(GrpcBgsClientKeys.GsmsProxyOp);
  if (canUse(cl)) {
    return cl as GsmsProxyOpClient;
  }
  return null;
};

export const getBgsGsmsProxyOpSafely = () => {
  let cl = getBgsGsmsProxyOp();
  if (!canUse(cl)) {
    cl = registerBgsGsmsProxyOp();
  }
  return cl!
};

/****************************************************************************************/
/*                               bgs thirdparty                                         */
/****************************************************************************************/

export const registerBgsThirdpartyOp = () => {
  const srv = BackendServices.BGS;
  let cl = getBgsThirdpartyOp();
  if (cl == null || cl === undefined) {
    cl = new ThirdpartyOpClient(getGrpcAddress(srv)()!, credentials.createInsecure());
    setGrpcClient(srv)(GrpcBgsClientKeys.ThirdpartyOp, cl);
  }
  return cl;
};

export const getBgsThirdpartyOp = () => {
  const cl = getGrpcClient(BackendServices.BGS)(GrpcBgsClientKeys.ThirdpartyOp);
  if (canUse(cl)) {
    return cl as ThirdpartyOpClient;
  }
  return null;
};

export const getBgsThirdpartyOpSafely = () => {
  let cl = getBgsThirdpartyOp();
  if (!canUse(cl)) {
    cl = registerBgsThirdpartyOp();
  }
  return cl!
};


/****************************************************************************************/
/*                               bgs store                                              */
/****************************************************************************************/

export const registerBgsStoreOp = () => {
  const srv = BackendServices.BGS;
  let cl = getBgsStoreOp();
  if (cl == null || cl === undefined) {
    cl = new StoreOpClient(getGrpcAddress(srv)()!, credentials.createInsecure());
    setGrpcClient(srv)(GrpcBgsClientKeys.StoreOp, cl);
  }
  return cl;
};

export const getBgsStoreOp = () => {
  const cl = getGrpcClient(BackendServices.BGS)(GrpcBgsClientKeys.StoreOp);
  if (canUse(cl)) {
    return cl as StoreOpClient;
  }
  return null;
};

export const getBgsStoreOpSafely = () => {
  let cl = getBgsStoreOp();
  if (!canUse(cl)) {
    cl = registerBgsStoreOp();
  }
  return cl!
};

/****************************************************************************************/
/*                               bgs store_fan                                           */
/****************************************************************************************/

export const registerBgsStoreFanOp = () => {
  const srv = BackendServices.BGS;
  let cl = getBgsStoreFanOp();
  if (cl == null || cl === undefined) {
    cl = new StoreFanOpClient(getGrpcAddress(srv)()!, credentials.createInsecure());
    setGrpcClient(srv)(GrpcBgsClientKeys.StoreFanOp, cl);
  }
  return cl;
};

export const getBgsStoreFanOp = () => {
  const cl = getGrpcClient(BackendServices.BGS)(GrpcBgsClientKeys.StoreFanOp);
  if (canUse(cl)) {
    return cl as StoreFanOpClient;
  }
  return null;
};

export const getBgsStoreFanOpSafely = () => {
  let cl = getBgsStoreFanOp();
  if (!canUse(cl)) {
    cl = registerBgsStoreFanOp();
  }
  return cl!
};

/****************************************************************************************/
/*                               bgs store_follow                                        */
/****************************************************************************************/

export const registerBgsStoreFollowOp = () => {
  const srv = BackendServices.BGS;
  let cl = getBgsStoreFollowOp();
  if (cl == null || cl === undefined) {
    cl = new StoreFollowOpClient(getGrpcAddress(srv)()!, credentials.createInsecure());
    setGrpcClient(srv)(GrpcBgsClientKeys.StoreFollowOp, cl);
  }
  return cl;
};

export const getBgsStoreFollowOp = () => {
  const cl = getGrpcClient(BackendServices.BGS)(GrpcBgsClientKeys.StoreFollowOp);
  if (canUse(cl)) {
    return cl as StoreFollowOpClient;
  }
  return null;
};

export const getBgsStoreFollowOpSafely = () => {
  let cl = getBgsStoreFollowOp();
  if (!canUse(cl)) {
    cl = registerBgsStoreFollowOp();
  }
  return cl!
};


/****************************************************************************************/
/*                               bgs store_endorse_request                              */
/****************************************************************************************/

export const registerBgsStoreEndorseRequestOp = () => {
  const srv = BackendServices.BGS;
  let cl = getBgsStoreEndorseRequestOp();
  if (cl == null || cl === undefined) {
    cl = new StoreEndorseRequestOpClient(getGrpcAddress(srv)()!, credentials.createInsecure());
    setGrpcClient(srv)(GrpcBgsClientKeys.StoreEndorseRequestOp, cl);
  }
  return cl;
};

export const getBgsStoreEndorseRequestOp = () => {
  const cl = getGrpcClient(BackendServices.BGS)(GrpcBgsClientKeys.StoreEndorseRequestOp);
  if (canUse(cl)) {
    return cl as StoreEndorseRequestOpClient;
  }
  return null;
};

export const getBgsStoreEndorseRequestOpSafely = () => {
  let cl = getBgsStoreEndorseRequestOp();
  if (!canUse(cl)) {
    cl = registerBgsStoreEndorseRequestOp();
  }
  return cl!
};


/****************************************************************************************/
/*                               bgs store_spokesman                                    */
/****************************************************************************************/

export const registerBgsStoreSpokesmanOp = () => {
  const srv = BackendServices.BGS;
  let cl = getBgsStoreSpokesmanOp();
  if (cl == null || cl === undefined) {
    cl = new StoreSpokesmanOpClient(getGrpcAddress(srv)()!, credentials.createInsecure());
    setGrpcClient(srv)(GrpcBgsClientKeys.StoreSpokesmanOp, cl);
  }
  return cl;
};

export const getBgsStoreSpokesmanOp = () => {
  const cl = getGrpcClient(BackendServices.BGS)(GrpcBgsClientKeys.StoreSpokesmanOp);
  if (canUse(cl)) {
    return cl as StoreSpokesmanOpClient;
  }
  return null;
};

export const getBgsStoreSpokesmanOpSafely = () => {
  let cl = getBgsStoreSpokesmanOp();
  if (!canUse(cl)) {
    cl = registerBgsStoreSpokesmanOp();
  }
  return cl!
};

/****************************************************************************************/
/*                               bgs sync                                               */
/****************************************************************************************/

export const registerBgsSyncOp = () => {
  const srv = BackendServices.BGS;
  let cl = getBgsSyncOp();
  if (cl == null || cl === undefined) {
    cl = new SyncOpClient(getGrpcAddress(srv)()!, credentials.createInsecure());
    setGrpcClient(srv)(GrpcBgsClientKeys.SyncOp, cl);
  }
  return cl;
};

export const getBgsSyncOp = () => {
  const cl = getGrpcClient(BackendServices.BGS)(GrpcBgsClientKeys.SyncOp);
  if (canUse(cl)) {
    return cl as SyncOpClient;
  }
  return null;
};

export const getBgsSyncOpSafely = () => {
  let cl = getBgsSyncOp();
  if (!canUse(cl)) {
    cl = registerBgsSyncOp();
  }
  return cl!
};

/****************************************************************************************/
/*                               bgs user                                               */
/****************************************************************************************/

export const registerBgsUserOp = () => {
  const srv = BackendServices.BGS;
  let cl = getBgsUserOp();
  if (cl == null || cl === undefined) {
    cl = new UserOpClient(getGrpcAddress(srv)()!, credentials.createInsecure());
    setGrpcClient(srv)(GrpcBgsClientKeys.UserOp, cl);
  }
  return cl;
};

export const getBgsUserOp = () => {
  const cl = getGrpcClient(BackendServices.BGS)(GrpcBgsClientKeys.UserOp);
  if (canUse(cl)) {
    return cl as UserOpClient;
  }
  return null;
};

export const getBgsUserOpSafely = () => {
  let cl = getBgsUserOp();
  if (!canUse(cl)) {
    cl = registerBgsUserOp();
  }
  return cl!
};

/****************************************************************************************/
/*                               bgs user_pv_log                                        */
/****************************************************************************************/

export const registerBgsUserPvLogOp = () => {
  const srv = BackendServices.BGS;
  let cl = getBgsUserPvLogOp();
  if (cl == null || cl === undefined) {
    cl = new UserPvLogOpClient(getGrpcAddress(srv)()!, credentials.createInsecure());
    setGrpcClient(srv)(GrpcBgsClientKeys.UserPvLogOp, cl);
  }
  return cl;
};

export const getBgsUserPvLogOp = () => {
  const cl = getGrpcClient(BackendServices.BGS)(GrpcBgsClientKeys.UserPvLogOp);
  if (canUse(cl)) {
    return cl as UserPvLogOpClient;
  }
  return null;
};

export const getBgsUserPvLogOpSafely = () => {
  let cl = getBgsUserPvLogOp();
  if (!canUse(cl)) {
    cl = registerBgsUserPvLogOp();
  }
  return cl!
};

/****************************************************************************************/
/*                               bgs user_fan                                           */
/****************************************************************************************/

export const registerBgsUserFanOp = () => {
  const srv = BackendServices.BGS;
  let cl = getBgsUserFanOp();
  if (cl == null || cl === undefined) {
    cl = new UserFanOpClient(getGrpcAddress(srv)()!, credentials.createInsecure());
    setGrpcClient(srv)(GrpcBgsClientKeys.UserFanOp, cl);
  }
  return cl;
};

export const getBgsUserFanOp = () => {
  const cl = getGrpcClient(BackendServices.BGS)(GrpcBgsClientKeys.UserFanOp);
  if (canUse(cl)) {
    return cl as UserFanOpClient;
  }
  return null;
};

export const getBgsUserFanOpSafely = () => {
  let cl = getBgsUserFanOp();
  if (!canUse(cl)) {
    cl = registerBgsUserFanOp();
  }
  return cl!
};

/****************************************************************************************/
/*                               bgs user_follow                                        */
/****************************************************************************************/

export const registerBgsUserFollowOp = () => {
  const srv = BackendServices.BGS;
  let cl = getBgsUserFollowOp();
  if (cl == null || cl === undefined) {
    cl = new UserFollowOpClient(getGrpcAddress(srv)()!, credentials.createInsecure());
    setGrpcClient(srv)(GrpcBgsClientKeys.UserFollowOp, cl);
  }
  return cl;
};

export const getBgsUserFollowOp = () => {
  const cl = getGrpcClient(BackendServices.BGS)(GrpcBgsClientKeys.UserFollowOp);
  if (canUse(cl)) {
    return cl as UserFollowOpClient;
  }
  return null;
};

export const getBgsUserFollowOpSafely = () => {
  let cl = getBgsUserFollowOp();
  if (!canUse(cl)) {
    cl = registerBgsUserFollowOp();
  }
  return cl!
};

/****************************************************************************************/
/*                               bgs brand                                              */
/****************************************************************************************/

export const registerBgsBrandOp = () => {
  const srv = BackendServices.BGS;
  let cl = getBgsBrandOp();
  if (cl == null || cl === undefined) {
    cl = new BrandOpClient(getGrpcAddress(srv)()!, credentials.createInsecure());
    setGrpcClient(srv)(GrpcBgsClientKeys.BrandOp, cl);
  }
  return cl;
};

export const getBgsBrandOp = () => {
  const cl = getGrpcClient(BackendServices.BGS)(GrpcBgsClientKeys.BrandOp);
  if (canUse(cl)) {
    return cl as BrandOpClient;
  }
  return null;
};

export const getBgsBrandOpSafely = () => {
  let cl = getBgsBrandOp();
  if (!canUse(cl)) {
    cl = registerBgsBrandOp();
  }
  return cl!
};

/****************************************************************************************/
/*                               bgs brand_admin                                              */
/****************************************************************************************/

export const registerBgsBrandAdminOp = () => {
  const srv = BackendServices.BGS;
  let cl = getBgsBrandAdminOp();
  if (cl == null || cl === undefined) {
    cl = new BrandAdminOpClient(getGrpcAddress(srv)()!, credentials.createInsecure());
    setGrpcClient(srv)(GrpcBgsClientKeys.BrandAdminOp, cl);
  }
  return cl;
};

export const getBgsBrandAdminOp = () => {
  const cl = getGrpcClient(BackendServices.BGS)(GrpcBgsClientKeys.BrandAdminOp);
  if (canUse(cl)) {
    return cl as BrandAdminOpClient;
  }
  return null;
};

export const getBgsBrandAdminOpSafely = () => {
  let cl = getBgsBrandAdminOp();
  if (!canUse(cl)) {
    cl = registerBgsBrandAdminOp();
  }
  return cl!
};

/****************************************************************************************/
/*                               bgs brand_distribute_request                           */
/****************************************************************************************/

export const registerBgsBrandDistributeRequestOp = () => {
  const srv = BackendServices.BGS;
  let cl = getBgsBrandDistributeRequestOp();
  if (cl == null || cl === undefined) {
    cl = new BrandDistributeRequestOpClient(getGrpcAddress(srv)()!, credentials.createInsecure());
    setGrpcClient(srv)(GrpcBgsClientKeys.BrandDistributeRequestOp, cl);
  }
  return cl;
};

export const getBgsBrandDistributeRequestOp = () => {
  const cl = getGrpcClient(BackendServices.BGS)(GrpcBgsClientKeys.BrandDistributeRequestOp);
  if (canUse(cl)) {
    return cl as BrandDistributeRequestOpClient;
  }
  return null;
};

export const getBgsBrandDistributeRequestOpSafely = () => {
  let cl = getBgsBrandDistributeRequestOp();
  if (!canUse(cl)) {
    cl = registerBgsBrandDistributeRequestOp();
  }
  return cl!
};

/****************************************************************************************/
/*                               bgs brand_endorse_request                              */
/****************************************************************************************/

export const registerBgsBrandEndorseRequestOp = () => {
  const srv = BackendServices.BGS;
  let cl = getBgsBrandEndorseRequestOp();
  if (cl == null || cl === undefined) {
    cl = new BrandEndorseRequestOpClient(getGrpcAddress(srv)()!, credentials.createInsecure());
    setGrpcClient(srv)(GrpcBgsClientKeys.BrandEndorseRequestOp, cl);
  }
  return cl;
};

export const getBgsBrandEndorseRequestOp = () => {
  const cl = getGrpcClient(BackendServices.BGS)(GrpcBgsClientKeys.BrandEndorseRequestOp);
  if (canUse(cl)) {
    return cl as BrandEndorseRequestOpClient;
  }
  return null;
};

export const getBgsBrandEndorseRequestOpSafely = () => {
  let cl = getBgsBrandEndorseRequestOp();
  if (!canUse(cl)) {
    cl = registerBgsBrandEndorseRequestOp();
  }
  return cl!
};

/****************************************************************************************/
/*                               bgs brand_fan                                           */
/****************************************************************************************/

export const registerBgsBrandFanOp = () => {
  const srv = BackendServices.BGS;
  let cl = getBgsBrandFanOp();
  if (cl == null || cl === undefined) {
    cl = new BrandFanOpClient(getGrpcAddress(srv)()!, credentials.createInsecure());
    setGrpcClient(srv)(GrpcBgsClientKeys.BrandFanOp, cl);
  }
  return cl;
};

export const getBgsBrandFanOp = () => {
  const cl = getGrpcClient(BackendServices.BGS)(GrpcBgsClientKeys.BrandFanOp);
  if (canUse(cl)) {
    return cl as BrandFanOpClient;
  }
  return null;
};

export const getBgsBrandFanOpSafely = () => {
  let cl = getBgsBrandFanOp();
  if (!canUse(cl)) {
    cl = registerBgsBrandFanOp();
  }
  return cl!
};

/****************************************************************************************/
/*                               bgs brand_follow                                        */
/****************************************************************************************/

export const registerBgsBrandFollowOp = () => {
  const srv = BackendServices.BGS;
  let cl = getBgsBrandFollowOp();
  if (cl == null || cl === undefined) {
    cl = new BrandFollowOpClient(getGrpcAddress(srv)()!, credentials.createInsecure());
    setGrpcClient(srv)(GrpcBgsClientKeys.BrandFollowOp, cl);
  }
  return cl;
};

export const getBgsBrandFollowOp = () => {
  const cl = getGrpcClient(BackendServices.BGS)(GrpcBgsClientKeys.BrandFollowOp);
  if (canUse(cl)) {
    return cl as BrandFollowOpClient;
  }
  return null;
};

export const getBgsBrandFollowOpSafely = () => {
  let cl = getBgsBrandFollowOp();
  if (!canUse(cl)) {
    cl = registerBgsBrandFollowOp();
  }
  return cl!
};

/****************************************************************************************/
/*                               bgs brand_bank_card                                    */
/****************************************************************************************/

export const registerBgsBrandBankCardOp = () => {
  const srv = BackendServices.BGS;
  let cl = getBgsBrandBankCardOp();
  if (cl == null || cl === undefined) {
    cl = new BrandBankCardOpClient(getGrpcAddress(srv)()!, credentials.createInsecure());
    setGrpcClient(srv)(GrpcBgsClientKeys.BrandBankCardOp, cl);
  }
  return cl;
};

export const getBgsBrandBankCardOp = () => {
  const cl = getGrpcClient(BackendServices.BGS)(GrpcBgsClientKeys.BrandBankCardOp);
  if (canUse(cl)) {
    return cl as BrandBankCardOpClient;
  }
  return null;
};

export const getBgsBrandBankCardOpSafely = () => {
  let cl = getBgsBrandBankCardOp();
  if (!canUse(cl)) {
    cl = registerBgsBrandBankCardOp();
  }
  return cl!
};


/****************************************************************************************/
/*                               bgs brand_distributor                                  */
/****************************************************************************************/

export const registerBgsBrandDistributorOp = () => {
  const srv = BackendServices.BGS;
  let cl = getBgsBrandDistributorOp();
  if (cl == null || cl === undefined) {
    cl = new BrandDistributorOpClient(getGrpcAddress(srv)()!, credentials.createInsecure());
    setGrpcClient(srv)(GrpcBgsClientKeys.BrandDistributorOp, cl);
  }
  return cl;
};

export const getBgsBrandDistributorOp = () => {
  const cl = getGrpcClient(BackendServices.BGS)(GrpcBgsClientKeys.BrandDistributorOp);
  if (canUse(cl)) {
    return cl as BrandDistributorOpClient;
  }
  return null;
};

export const getBgsBrandDistributorOpSafely = () => {
  let cl = getBgsBrandDistributorOp();
  if (!canUse(cl)) {
    cl = registerBgsBrandDistributorOp();
  }
  return cl!
};

/****************************************************************************************/
/*                               bgs brand_spokesman                                    */
/****************************************************************************************/

export const registerBgsBrandSpokesmanOp = () => {
  const srv = BackendServices.BGS;
  let cl = getBgsBrandSpokesmanOp();
  if (cl == null || cl === undefined) {
    cl = new BrandSpokesmanOpClient(getGrpcAddress(srv)()!, credentials.createInsecure());
    setGrpcClient(srv)(GrpcBgsClientKeys.BrandSpokesmanOp, cl);
  }
  return cl;
};

export const getBgsBrandSpokesmanOp = () => {
  const cl = getGrpcClient(BackendServices.BGS)(GrpcBgsClientKeys.BrandSpokesmanOp);
  if (canUse(cl)) {
    return cl as BrandSpokesmanOpClient;
  }
  return null;
};

export const getBgsBrandSpokesmanOpSafely = () => {
  let cl = getBgsBrandSpokesmanOp();
  if (!canUse(cl)) {
    cl = registerBgsBrandSpokesmanOp();
  }
  return cl!
};

/****************************************************************************************/
/*                               bgs brand_warehouse                                    */
/****************************************************************************************/

export const registerBgsBrandWarehouseOp = () => {
  const srv = BackendServices.BGS;
  let cl = getBgsBrandWarehouseOp();
  if (cl == null || cl === undefined) {
    cl = new BrandWarehouseOpClient(getGrpcAddress(srv)()!, credentials.createInsecure());
    setGrpcClient(srv)(GrpcBgsClientKeys.BrandWarehouseOp, cl);
  }
  return cl;
};

export const getBgsBrandWarehouseOp = () => {
  const cl = getGrpcClient(BackendServices.BGS)(GrpcBgsClientKeys.BrandWarehouseOp);
  if (canUse(cl)) {
    return cl as BrandWarehouseOpClient;
  }
  return null;
};

export const getBgsBrandWarehouseOpSafely = () => {
  let cl = getBgsBrandWarehouseOp();
  if (!canUse(cl)) {
    cl = registerBgsBrandWarehouseOp();
  }
  return cl!
};

/****************************************************************************************/
/*                               bgs common                                             */
/****************************************************************************************/

export const registerBgsCommonOp = () => {
  const srv = BackendServices.BGS;
  let cl = getBgsCommonOp();
  if (cl == null || cl === undefined) {
    cl = new CommonOpClient(getGrpcAddress(srv)()!, credentials.createInsecure());
    setGrpcClient(srv)(GrpcBgsClientKeys.CommonOp, cl);
  }
  return cl;
};

export const getBgsCommonOp = () => {
  const cl = getGrpcClient(BackendServices.BGS)(GrpcBgsClientKeys.CommonOp);
  if (canUse(cl)) {
    return cl as CommonOpClient;
  }
  return null;
};

export const getBgsCommonOpSafely = () => {
  let cl = getBgsCommonOp();
  if (!canUse(cl)) {
    cl = registerBgsCommonOp();
  }
  return cl!
};

/****************************************************************************************/
/*                               bgs wxpay_applyment                                    */
/****************************************************************************************/

export const registerBgsWxpayApplymentOp = () => {
  const srv = BackendServices.BGS;
  let cl = getBgsWxpayApplymentOp();
  if (cl == null || cl === undefined) {
    cl = new WxpayApplymentOpClient(getGrpcAddress(srv)()!, credentials.createInsecure());
    setGrpcClient(srv)(GrpcBgsClientKeys.WxpayApplymentOp, cl);
  }
  return cl;
};

export const getBgsWxpayApplymentOp = () => {
  const cl = getGrpcClient(BackendServices.BGS)(GrpcBgsClientKeys.WxpayApplymentOp);
  if (canUse(cl)) {
    return cl as WxpayApplymentOpClient;
  }
  return null;
};

export const getBgsWxpayApplymentOpSafely = () => {
  let cl = getBgsWxpayApplymentOp();
  if (!canUse(cl)) {
    cl = registerBgsWxpayApplymentOp();
  }
  return cl!
};

/****************************************************************************************/
/*                               bgs product_group                                      */
/****************************************************************************************/

export const registerBgsProductGroupOp = () => {
  const srv = BackendServices.BGS;
  let cl = getBgsProductGroupOp();
  if (cl == null || cl === undefined) {
    cl = new ProductGroupOpClient(getGrpcAddress(srv)()!, credentials.createInsecure());
    setGrpcClient(srv)(GrpcBgsClientKeys.ProductGroupOp, cl);
  }
  return cl;
};

export const getBgsProductGroupOp = () => {
  const cl = getGrpcClient(BackendServices.BGS)(GrpcBgsClientKeys.ProductGroupOp);
  if (canUse(cl)) {
    return cl as ProductGroupOpClient;
  }
  return null;
};

export const getBgsProductGroupOpSafely = () => {
  let cl = getBgsProductGroupOp();
  if (!canUse(cl)) {
    cl = registerBgsProductGroupOp();
  }
  return cl!
};

/****************************************************************************************/
/*                               bgs product                                            */
/****************************************************************************************/

export const registerBgsProductOp = () => {
  const srv = BackendServices.BGS;
  let cl = getBgsProductOp();
  if (cl == null || cl === undefined) {
    cl = new ProductOpClient(getGrpcAddress(srv)()!, credentials.createInsecure());
    setGrpcClient(srv)(GrpcBgsClientKeys.ProductOp, cl);
  }
  return cl;
};

export const getBgsProductOp = () => {
  const cl = getGrpcClient(BackendServices.BGS)(GrpcBgsClientKeys.ProductOp);
  if (canUse(cl)) {
    return cl as ProductOpClient;
  }
  return null;
};

export const getBgsProductOpSafely = () => {
  let cl = getBgsProductOp();
  if (!canUse(cl)) {
    cl = registerBgsProductOp();
  }
  return cl!
};

/****************************************************************************************/
/*                               bgs activity                                           */
/****************************************************************************************/

export const registerBgsActivityOp = () => {
  const srv = BackendServices.BGS;
  let cl = getBgsActivityOp();
  if (cl == null || cl === undefined) {
    cl = new ActivityOpClient(getGrpcAddress(srv)()!, credentials.createInsecure());
    setGrpcClient(srv)(GrpcBgsClientKeys.ActivityOp, cl);
  }
  return cl;
};

export const getBgsActivityOp = () => {
  const cl = getGrpcClient(BackendServices.BGS)(GrpcBgsClientKeys.ActivityOp);
  if (canUse(cl)) {
    return cl as ActivityOpClient;
  }
  return null;
};

export const getBgsActivityOpSafely = () => {
  let cl = getBgsActivityOp();
  if (!canUse(cl)) {
    cl = registerBgsActivityOp();
  }
  return cl!
};

/****************************************************************************************/
/*                               bgs activity_user_award                                */
/****************************************************************************************/

export const registerBgsActivityUserAwardOp = () => {
  const srv = BackendServices.BGS;
  let cl = getBgsActivityUserAwardOp();
  if (cl == null || cl === undefined) {
    cl = new ActivityUserAwardOpClient(getGrpcAddress(srv)()!, credentials.createInsecure());
    setGrpcClient(srv)(GrpcBgsClientKeys.ActivityUserAwardOp, cl);
  }
  return cl;
};

export const getBgsActivityUserAwardOp = () => {
  const cl = getGrpcClient(BackendServices.BGS)(GrpcBgsClientKeys.ActivityUserAwardOp);
  if (canUse(cl)) {
    return cl as ActivityUserAwardOpClient;
  }
  return null;
};

export const getBgsActivityUserAwardOpSafely = () => {
  let cl = getBgsActivityUserAwardOp();
  if (!canUse(cl)) {
    cl = registerBgsActivityUserAwardOp();
  }
  return cl!
};

/****************************************************************************************/
/*                               bgs activity_user_procedure                            */
/****************************************************************************************/

export const registerBgsActivityUserProcedureOp = () => {
  const srv = BackendServices.BGS;
  let cl = getBgsActivityUserProcedureOp();
  if (cl == null || cl === undefined) {
    cl = new ActivityUserProcedureOpClient(getGrpcAddress(srv)()!, credentials.createInsecure());
    setGrpcClient(srv)(GrpcBgsClientKeys.ActivityUserProcedureOp, cl);
  }
  return cl;
};

export const getBgsActivityUserProcedureOp = () => {
  const cl = getGrpcClient(BackendServices.BGS)(GrpcBgsClientKeys.ActivityUserProcedureOp);
  if (canUse(cl)) {
    return cl as ActivityUserProcedureOpClient;
  }
  return null;
};

export const getBgsActivityUserProcedureOpSafely = () => {
  let cl = getBgsActivityUserProcedureOp();
  if (!canUse(cl)) {
    cl = registerBgsActivityUserProcedureOp();
  }
  return cl!
};

/****************************************************************************************/
/*                               bgs activity_user_pv_log                               */
/****************************************************************************************/

export const registerBgsActivityUserPvLogOp = () => {
  const srv = BackendServices.BGS;
  let cl = getBgsActivityUserPvLogOp();
  if (cl == null || cl === undefined) {
    cl = new ActivityUserPvLogOpClient(getGrpcAddress(srv)()!, credentials.createInsecure());
    setGrpcClient(srv)(GrpcBgsClientKeys.ActivityUserPvLogOp, cl);
  }
  return cl;
};

export const getBgsActivityUserPvLogOp = () => {
  const cl = getGrpcClient(BackendServices.BGS)(GrpcBgsClientKeys.ActivityUserPvLogOp);
  if (canUse(cl)) {
    return cl as ActivityUserPvLogOpClient;
  }
  return null;
};

export const getBgsActivityUserPvLogOpSafely = () => {
  let cl = getBgsActivityUserPvLogOp();
  if (!canUse(cl)) {
    cl = registerBgsActivityUserPvLogOp();
  }
  return cl!
};

/****************************************************************************************/
/*                               bgs activity_stat                                      */
/****************************************************************************************/

export const registerBgsActivityStatOp = () => {
  const srv = BackendServices.BGS;
  let cl = getBgsActivityStatOp();
  if (cl == null || cl === undefined) {
    cl = new ActivityStatOpClient(getGrpcAddress(srv)()!, credentials.createInsecure());
    setGrpcClient(srv)(GrpcBgsClientKeys.ActivityStatOp, cl);
  }
  return cl;
};

export const getBgsActivityStatOp = () => {
  const cl = getGrpcClient(BackendServices.BGS)(GrpcBgsClientKeys.ActivityStatOp);
  if (canUse(cl)) {
    return cl as ActivityStatOpClient;
  }
  return null;
};

export const getBgsActivityStatOpSafely = () => {
  let cl = getBgsActivityStatOp();
  if (!canUse(cl)) {
    cl = registerBgsActivityStatOp();
  }
  return cl!
};

/****************************************************************************************/
/*                               bgs order                                              */
/****************************************************************************************/

export const registerBgsOrderOp = () => {
  const srv = BackendServices.BGS;
  let cl = getBgsOrderOp();
  if (cl == null || cl === undefined) {
    cl = new OrderOpClient(getGrpcAddress(srv)()!, credentials.createInsecure());
    setGrpcClient(srv)(GrpcBgsClientKeys.OrderOp, cl);
  }
  return cl;
};

export const getBgsOrderOp = () => {
  const cl = getGrpcClient(BackendServices.BGS)(GrpcBgsClientKeys.OrderOp);
  if (canUse(cl)) {
    return cl as OrderOpClient;
  }
  return null;
};

export const getBgsOrderOpSafely = () => {
  let cl = getBgsOrderOp();
  if (!canUse(cl)) {
    cl = registerBgsOrderOp();
  }
  return cl!
};

/****************************************************************************************/
/*                               bgs after_sale                                         */
/****************************************************************************************/

export const registerBgsAfterSaleOp = () => {
  const srv = BackendServices.BGS;
  let cl = getBgsAfterSaleOp();
  if (cl == null || cl === undefined) {
    cl = new AfterSaleOpClient(getGrpcAddress(srv)()!, credentials.createInsecure());
    setGrpcClient(srv)(GrpcBgsClientKeys.AfterSaleOp, cl);
  }
  return cl;
};

export const getBgsAfterSaleOp = () => {
  const cl = getGrpcClient(BackendServices.BGS)(GrpcBgsClientKeys.AfterSaleOp);
  if (canUse(cl)) {
    return cl as AfterSaleOpClient;
  }
  return null;
};

export const getBgsAfterSaleOpSafely = () => {
  let cl = getBgsAfterSaleOp();
  if (!canUse(cl)) {
    cl = registerBgsAfterSaleOp();
  }
  return cl!
};